"""
Case Type   : gs_checkse工具功能
Case Name   : 验证gs_checkse的A1检测项确保没有host条目使用md5认证检测与修复成功
Create At   : 2024/10/23
Owner       : lihongji
Description :
    1.设置host条目存在md5认证后检测A1项
    2.设置host条目为sha256认证后检测A1项
    3.查看dn目录下的pg_hba.conf文件host条目认证方式
Expect      :
    1.设置成功，打印告警信息
    2.设置成功，不打印告警信息
    3.host条目认证方式不存在md5
History     :
"""
import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro


class GS_CHECKSE(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.primaryNode = Node('PrimaryDbUser')
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.env_path = macro.DB_ENV_PATH
        self.pg_hba_path = os.path.join(macro.DB_INSTANCE_PATH,
                                        macro.PG_HBA_FILE_NAME)

    def test_gscheckse(self):
        step = ('----step1:设置host条目存在md5认证后检测A1项 '
                'expect:设置成功，打印告警信息----')
        self.log.info(step)
        excute_msg = self.sh_primary.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, param='',
            pghba_param=f'host all flag {self.primaryNode.db_host}/32 md5')
        self.assertTrue(excute_msg)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A1 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn("Ensure there are no 'host' entries using MD5 "
                      "authentication.", check_res, '执行失败' + step)

        step = ('----step2:设置host条目为sha256认证后检测A1项 '
                'expect:设置成功，不打印告警信息----')
        self.log.info(step)
        excute_msg = self.sh_primary.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, param='',
            pghba_param=f'host all flag {self.primaryNode.db_host}/32 sha256')
        self.assertTrue(excute_msg)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A1 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertNotIn("Ensure there are no 'host' entries using MD5 "
                         "authentication.", check_res, '执行失败' + step)

        step = ('----step3:查看dn目录下的pg_hba.conf文件host条目认证方式'
                'expect:host条目认证方式不存在md5----')
        self.log.info(step)
        gs_checkse_cmd = (f"grep -P '^[^#]*host(ssl|nossl)?\s+.+[Mm][Dd][5]\s*$' "
                          f"{macro.DB_INSTANCE_PATH}/{macro.PG_HBA_FILE_NAME}")
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIsNot(check_res, '执行失败' + step)

    def tearDown(self):
        step = '----step6:清理环境  expect:成功----'
        self.log.info(step)
        del_flag = (f"sed -i '/flag/d' {self.pg_hba_path };"
                    f"cat {self.pg_hba_path} | grep flag")
        self.log.info(del_flag)
        check_res = self.common.get_sh_result(self.primaryNode, del_flag)
        self.assertNotIn('flag', check_res, '执行失败' + step)
        self.log.info(f'----{os.path.basename(__file__)} end----')